package vip.specimen.system.datasource.validation.implement;

import vip.specimen.system.datasource.entity.SystemArticleCategoriesEntity;
import vip.specimen.system.datasource.validation.annotation.CategoriesSuperiorNotSelf;
import vip.specimen.system.service.SystemArticleCategoriesService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.UUID;

/**
 * *@Author: 余浪人
 * *@Date：2021/4/26 17:13
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
public class CategoriesSuperiorNotSelfImp implements ConstraintValidator<CategoriesSuperiorNotSelf, UUID> {
    @Resource
    private HttpServletRequest request;
    @Resource
    private SystemArticleCategoriesService articleCategoriesService;

    @Override
    public boolean isValid(UUID uuid, ConstraintValidatorContext constraintValidatorContext) {
        if (null == uuid) return true;
        SystemArticleCategoriesEntity categoriesEntity = this.articleCategoriesService.findById(uuid);
        if (categoriesEntity.getUuid().toString().equals(request.getParameter("uuid"))) return false;
        return valid(categoriesEntity); //递归查询 是否是子级
    }

    private boolean valid(SystemArticleCategoriesEntity columnEntity) {
        if (null == columnEntity.getSuperior()) return true;
        else if (columnEntity.getSuperior().getUuid().toString().equals(request.getParameter("uuid"))) return false;
        return valid(columnEntity.getSuperior());
    }
}
